# 导入模块
from flask import *

# 导入类型类
from stumodel import Student

# 学生数据
students = []

# 创建蓝图对象
stu = Blueprint("StuBluePrint",__name__,url_prefix="/stu")

@stu.route("/index")
def index():

    return render_template("index.html",students=students)


# 添加视图接口函数
@stu.route("/add", methods=['GET',"POST"])
def add():
    if request.method == "GET":
        return render_template("add.html")
    else:
        # 获取表单提交的数据
        name = request.values.get("name")
        age = int(request.values.get("age"))
        gender = request.values.get("gender")
        address = request.values.get("address")
        # 创建实例对象
        obj = Student(name=name, age=age, gender=gender, address=address)
        # 获取会话对象
        # 插入数据
        students.append(obj)
        return redirect("/stu/index")

# 修改视图接口函数
@stu.route("/change/<sid>", methods=['GET',"POST"])
def change(sid):
    if request.method == "GET":
        cstu = ""
        for stu in students:
            if stu.sid == int(sid):
                cstu = stu
                print(cstu)
                break
        print(cstu)
        return render_template("change.html",stu=cstu)
    else:
        # 获取表单提交的数据
        name = request.values.get("name")
        age = int(request.values.get("age"))
        gender = request.values.get("gender")
        address = request.values.get("address")
        for stu in students:
            if stu.sid == int(sid):
                stu.name = name
                stu.age = age
                stu.gender = gender
                stu.address = address
                break
        return redirect("/stu/index")

# 删除视图接口函数
@stu.route("/del/<sid>")
def delete(sid):
    for stu in students:
        if stu.sid == int(sid):
            students.remove(stu)
            break
    return redirect("/stu/index")
